var app = getApp();
Page({

  data: {
    showgif:true,
    info:[],
    choose:'',
    itemList: ['正常', '返工','报废'],
    status:'点击选择状态',
    userInfo: null,
    title:'',
    buttom:'',
    zhicheng:'',
    lid:'9999999',
    page:'',
    groupid:'',
    cando:true,//默认可以提交，如果在提交时，为返回结果时，禁止再次点击提交
  },

  onLoad: function (options) {
    var param = options.scene.split("_");
    var lid = param[0];
    var page = param[1];
  
    this.setData({ lid: lid,page:page });
    this.getlist(lid,page)
    
     // this.setData({ lid: 58 });
      //this.getlist(58)

  },

  onShow: function () {
    if (!wx.getStorageSync('userInfo')) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }else{

      this.setData({ userInfo:wx.getStorageSync('userInfo')});

      this.setData({ title:wx.getStorageSync('title')});

      if(wx.getStorageSync('gerenziliao')){

        this.setData({ userinfo:wx.getStorageSync('gerenziliao')});
      }
    

  
    }
  },

  changezt(){
    var that = this
    wx.showActionSheet({
      itemList: ['正常', '返工','报废'],
      success(res) {
        that.setData({ status: that.data.itemList[res.tapIndex]});
        that.setData({ choose: res.tapIndex+1});    
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  formSubmit: function (event) {
    var that = this;
 
    if(!that.data.cando){
      app.error('数据正常提交中，请勿频繁操作');
      return false;
    }else{
    

      if(that.data.buttom == '领料'){

        if (event.detail.value.num > 0) {
       
        }else{
          app.error('领料数量不能为空');
          return;
        }

        //正常提交数据
        that.setData({ cando:false});
        that.setData({showgif:true});
        wx.request({
          url: app.apiUrl + '/api/data/getproduce',
          data: {
            id: that.data.lid,
            openid:wx.getStorageSync('openid'),
            groupid: that.data.info.groupid,
            count:event.detail.value.num,
             ban:wx.getStorageSync('banci')
          },
          method: 'POST',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          success: res=> {
            //回复正常提交数据
            that.setData({ cando:true});
            that.setData({showgif:false});
           if(res.data.code == 1){
            wx.showModal({
              title: '温馨提示',
              content: res.data.msg,
              showCancel: false
            });
  
            setTimeout(function () {
              wx.redirectTo({
                url: '/page/my/index',
              })
            }, 1500)
  
            return false
           }else{
            wx.showModal({
              title: '温馨提示',
              content: res.data.msg,
              showCancel: false
            });
           }
    
          }
        })
  
      }
      if(that.data.buttom == '提交'){
      
        //如果是包装 提交需要 输入包的实际数量
        var bnum = 0;
        if(that.data.groupid == 24){
            if(event.detail.value.bnum>0){
              bnum = event.detail.value.bnum;
            }else{
              app.error('请输入纸箱/材料膜数量');
              return;
            }
        }  
        
        var choose = that.data.choose
        if(!choose){
          app.error('请选择状态');
          return;
        }
  
        if(event.detail.value.num>0 && event.detail.value.num <= that.data.info.num){
  
        }else{
          app.error('请输入正确的数量');
          return;
        }
  
         if(choose == 1){
           if(event.detail.value.num != that.data.info.num){
            app.error('请输入正确的数量');
            return;
           }
         }

        //正常提交数据
        that.setData({ cando:false});
        that.setData({showgif:true});
     
        wx.request({
          url: app.apiUrl + '/api/data/comproduce',
          data: {
            id: that.data.lid,
            openid:wx.getStorageSync('openid'),
            style:wx.getStorageSync('jswstyle'),
            groupid: that.data.info.groupid,
            count2:event.detail.value.num,
            bnum:bnum,
            type:that.data.choose,
            count:that.data.info.num,
            llid:that.data.info.llid,
            page:that.data.info.ppage,
             ban:wx.getStorageSync('banci')
          },
          method: 'POST',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          success: res=> {
          //回复正常提交数据
          that.setData({ cando:true});
          that.setData({showgif:false});
           if(res.data.code == 1){
            wx.showModal({
              title: '温馨提示',
              content: res.data.msg,
              showCancel: false
            });
  
            setTimeout(function () {
              wx.redirectTo({
                url: '/page/my/index',
              })
            }, 1500)
  
            return false
           }else{
            wx.showModal({
              title: '温馨提示',
              content: res.data.msg,
              showCancel: false
            });
           }
    
          }
        })
  
  
      }
    }
  },

  getlist:function (lid,page){
    // var lid = 58
  
    var that = this
    wx.request({
      url: app.apiUrl + '/api/data/getFinshed',
      data: {
        openid:wx.getStorageSync('openid'),
        id:lid,
        page:page
      },
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success: function (res) {
        that.setData({showgif:false});
        if(res.data.code == 1){
          if(res.data.data.show == 0){
            //领料
            that.setData({ buttom: '领料'});

          }else{
            //提交
            that.setData({ buttom: '提交'});
          }
          that.setData({ info: res.data.data});
          that.setData({ groupid: res.data.data.groupid});
              
        }else{
          wx.showModal({
            title: '温馨提示',
               content: res.data.msg,
                 showCancel: false,
                 success:function(res){
                  wx.redirectTo({
                    url: '/page/my/index',
                  })
                 }

            });
        }
     
      }
    });
  },
  //上传头像
  uploadAvatar: function () {
    var that = this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var formData = app.globalData.config.upload.multipart;
        formData.token = app.globalData.userInfo.token;
        wx.uploadFile({
          url: app.globalData.config.upload.uploadurl,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: formData,
          success: function (res) {
            var row = JSON.parse(res.data);
            if (row.code == 1) {
              app.success('头像上传成功');              
              that.setData({ ["userInfo.avatar"]: app.globalData.config.upload.cdnurl + row.data.url });
            }
          },
          error: function (e) {
            console.log(e);
          }
        });
      }, error: function () {
        console.log(e);
      }
    });
  }

})